/**
 * Created by Jaye on 2017/12/22.
 */
$(function () {
    // //初始化table
    var oTable1 = $('#apiListTable').dataTable(
        {
            "bPaginage": true,//分页工具条显示
            "sPaginationType": "full_numbers",//分页工具条样式
            "bStateSave": true, //是否打开客户端状态记录功能,此功能在ajax刷新纪录的时候不会将个性化设定回复为初始化状态
            "bScrollCollapse": true, //当显示的数据不足以支撑表格的默认的高度
            "bLengthChange": true, //每页显示的记录数
            "bFilter": true, //搜索栏
            "bSort": true, //是否支持排序功能
            "bInfo": true, //显示表格信息
            "bAutoWidth": true, //自适应宽度
            "bJQueryUI": false,//是否开启主题
            "bDestroy": true,
            "bProcessing": true, //开启读取服务器数据时显示正在加载中……特别是大数据量的时候，开启此功能比较好
            "sAjaxDataProp": "data",//是服务器分页的标志，必须有
            "sAjaxSource": "/api/list",//通过ajax实现分页的url路径。
            "bServerSide": true,//服务器处理分页，默认是false，需要服务器处理，必须true
            "fnServerData": function (sSource, aoData, fnCallback) {
                $.ajax({
                    type: "post",
                    url: sSource,
                    dataType: "json",
                    data: {"aodata": JSON.stringify(aoData)},
                    success: function (resp) {
                        fnCallback(resp);
                    }
                });

            },
            "aoColumns": [//初始化要显示的列
                // {
                //     "mDataProp": "id",//获取列数据，跟服务器返回字段一致
                //     "sClass": "center",//显示样式
                //     "mRender": function (data, type, full) {//返回自定义的样式
                //         return "<label><input type='checkbox' class='ace' /><span class='lbl'></span></label>"
                //     }
                // },
                // {
                //     "title": "apiId",
                //     "sClass": "text-center",
                //     "mDataProp": "apiId"
                // },
                {
                    title: "API编号",
                    "sClass": "text-center",
                    "mDataProp": "apiId"
                },
                {
                    title: "API名称",
                    "sClass": "text-center",
                    "mDataProp": "apiName"
                },
                {
                    title: "URL",
                    "sClass": "text-center",
                    "mDataProp": "url"
                },
                {
                    title: "所属服务id",
                    "sClass": "text-center",
                    "mDataProp": "serviceId"
                },
                {
                    title: "所属服务",
                    "sClass": "text-center",
                    "mDataProp": "serviceName"
                },
                {
                    title: "所属服务名",
                    "sClass": "text-center",
                    "mDataProp": "serviceNameCn"
                }
            ],
            "aoColumnDefs": [{//用来设置列一些特殊列的属性
                "bSortable": false,
                "aTargets": [0, 1, 2, 3, 4, 5]
                //第一列不排序
            }
                // , {
                //     "bSortable": false,
                //     "aTargets": [1]
                // }, {
                //     "bSortable": false,
                //     "aTargets": [2]
                // }, {
                //     "bSortable": false,
                //     "aTargets": [3]
                // }, {
                //     "bSortable": false,
                //     "aTargets": [4]
                // }, {
                //     "bSortable": false,
                //     "aTargets": [6]
                // }
            ],
            "oLanguage": {//语言设置
                "sProcessing": "处理中...",
                "sLengthMenu": "显示 _MENU_ 项结果",
                "sZeroRecords": "没有匹配结果",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix": "",
                "sSearch": "搜索:",
                "sUrl": "",
                "sEmptyTable": "表中数据为空",
                "sLoadingRecords": "载入中...",
                "sInfoThousands": ",",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                }
                ,
                "oAria": {
                    "sSortAscending": ": 以升序排列此列",
                    "sSortDescending": ": 以降序排列此列"
                }
            }
        })
    ;

    //全选
    $('table th input:checkbox').on(
        'click',
        function () {
            var that = this;
            $(this).closest('table').find(
                'tr > td:first-child input:checkbox').each(
                function () {
                    this.checked = that.checked;
                    $(this).closest('tr').toggleClass('selected');
                });

        });

    $('#importApiModal').on('hide.bs.modal', function () {
        $('#service_name').val("");
        $('#raml_file').val("");
        location.reload();
    });
    $('#serviceModal').on('show.bs.modal', function () {
        $("#serviceTable").bootstrapTable({
            url: "/service/list",
            method: "post",
            height: "200",
            iconSize: "outline",
            search: true,
            showRefresh: true,
            showColumns: !0,
            singleSeclect: true,
            columns: [
                {radio: true},
                {
                    title: "服务Id",
                    field: "serviceId",
                    align: "center"
                }, {
                    title: "服务名称",
                    field: "serviceName",
                    align: "center"
                }],
            icons: {
                refresh: "glyphicon-repeat",
                toggle: "glyphicon-list-alt",
                columns: "glyphicon-list"
            }
        });
    });

});

function confirm_service() {
    var selectContent = $("#serviceTable").bootstrapTable('getSelections')[0];
    if (typeof(selectContent) == 'undefined') {
        // toastr.warning('请选择一列数据!');
        alert("请选择一列数据!");
        // $("#no_record").alert();
        return false;
    } else {
        // console.info(selectContent);
        $("#service_id").val(selectContent.serviceId);
        $("#service_name").val(selectContent.serviceName);
        $('#serviceModal').modal('hide');
        return;
    }
}
function upLoad() {
    // var uploading = false;
    var formData = new FormData($("#form_data")[0]);
    // if(uploading){
    //     alert("文件正在上传中，请稍候");
    //     return false;
    // }
    $.ajax({
        url: "/api/import",
        type: "post",
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        dataType: "json",
        beforeSend: function () {
            // uploading = true;
        },
        success: function (data) {
            if (data.code == 1000) {
                if (data.data.executeResult) {
                    swal({
                        title: "导入成功",
                        type: "success",
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "确认",
                        closeOnConfirm: false
                    }, function () {
                        $('#importApiModal').modal('hide');
                        location.reload();
                    });
                    return;
                }
            }
            swal({
                title: "导入失败",
                type: "error",
                confirmButtonText: "确认",
                closeOnConfirm: false
            }, function () {
                $('#importApiModal').modal('hide');
            });
            return;
        },
        error: function (returndata) {
            swal({
                title: "导入失败",
                type: "error",
                confirmButtonText: "确认",
                closeOnConfirm: false
            });
            $('#importApiModal').modal('hide');
        }
    });
}
